package com.yang.gps;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private TextView txtstart;
    private AlarmManager alarmManager;
    private PendingIntent pendingIntent;
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    public AMapLocationClientOption mLocationOption = null;
    private Context mcontent;
    private PendingIntent pi;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            AMapLocation  a = aMapLocation;
            System.out.println("定位成功"+a.getLatitude()+""+a.getLongitude());
            txtstart.setText("定位成功"+a.getLatitude()+""+a.getLongitude());

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mcontent = this;

        initView();

    }

    private void  initView(){

        txtstart = findViewById(R.id.txtstart);
        mLocationOption = new AMapLocationClientOption();
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
//设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        mLocationOption.setInterval(2000);
        mLocationClient.setLocationOption(mLocationOption);

        mLocationClient.startLocation();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mLocationClient.stopLocation();
        mLocationClient.onDestroy();
    }
}